package com.isunland.managesystem.adapter;

import android.support.annotation.NonNull;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import butterknife.BindView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseButterKnifeAdapter;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.Plotmappinginformation;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.MyUtils;
import java.util.ArrayList;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/4/5 15:01
 */

public class WorkAreaAdapter extends BaseButterKnifeAdapter<Plotmappinginformation>
    implements View.OnClickListener {

  /** 是否显示选择按钮 */
  private boolean mIsShowSelect;
  private Callback mCallback;
  @SuppressWarnings("unused") private int mIconRes;
  private int size;

  @SuppressWarnings("unused")
  public WorkAreaAdapter(BaseVolleyActivity context, ArrayList<Plotmappinginformation> objects) {
    super(context, objects);
  }

  public WorkAreaAdapter(BaseVolleyActivity context, ArrayList<Plotmappinginformation> objects,
      Callback callback, boolean mIsShowSelect) {
    super(context, objects);
    this.mCallback = callback;
    this.mIsShowSelect = mIsShowSelect;
  }

  @Override protected int setLayoutId() {
    return R.layout.adapter_work_area;
  }

  @NonNull @Override protected BaseViewHolder initHolder(View convertView) {
    return new ViewHolder(convertView);
  }

  @Override
  protected void initView(Plotmappinginformation item, BaseViewHolder baseHolder, int position) {
    ViewHolder holder = (ViewHolder) baseHolder;
    holder.tvTitle.setText(
        MyStringUtil.connect(context.getString(R.string.areaname1), item.getWplotDepict()));
    holder.tvContent1.setText(item.getRegion());
    if (MyStringUtil.equals("||", item.getRemark())) item.setRemark("");
    holder.tvContent2.setVisibility(
        MyStringUtil.isEmpty(item.getRemark()) ? View.GONE : View.VISIBLE);
    holder.tvContent2.setText(item.getRemark());
    MyUtils.switchDataStutus(context, holder.tvContent3, item.getDataStatus());
    holder.tvBottom.setText(
        MyStringUtil.connect(item.getTplotName(), MyStringUtil.SEPEREATE, item.getPlotArea(),
            context.getString(R.string.unitArea)));
    //noinspection deprecation
    holder.ivLogo.setText(MyStringUtil.substring(item.getRegion(), 0, 2));
    holder.ivMore.setTag(item);
    //holder.tvIndex.setText(MyStringUtil.connect("#", MyStringUtil.valueOf(position + 1)));
    //  1/144
    holder.tvIndex.setText((position + 1) + "/" + getCount());
  }

  @Override public boolean isEmpty() {
    return false;
  }

  @Override public void onClick(View view) {
    if (view == null || view.getTag() == null) return;
    Plotmappinginformation item = (Plotmappinginformation) view.getTag();
    mCallback.click(view, item);
  }

  private void showPopMenu(final View view) {

    if (view == null || view.getTag() == null) return;

    if (!(view.getTag() instanceof Plotmappinginformation)) return;
    final Plotmappinginformation itemObj = (Plotmappinginformation) view.getTag();
    PopupMenu popupMenu = new PopupMenu(context, view);
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
      @Override public boolean onMenuItemClick(MenuItem item) {
        mCallback.click(view, itemObj);
        return false;
      }
    });

    popupMenu.getMenu().addSubMenu("选择");
    popupMenu.show();
  }

  public int getSize() {
    return size;
  }

  public void setSize(int size) {
    this.size = size;
  }

  public interface Callback {
    void click(View v, Plotmappinginformation item);
  }

  protected class ViewHolder extends BaseViewHolder {
    @BindView(R.id.iv_logo) TextView ivLogo;
    @BindView(R.id.tv_title) TextView tvTitle;
    @BindView(R.id.tv_content1) TextView tvContent1;
    @BindView(R.id.tv_content2) TextView tvContent2;
    @BindView(R.id.tv_content3) TextView tvContent3;
    @BindView(R.id.tv_bottom) TextView tvBottom;
    @BindView(R.id.iv_more) ImageView ivMore;
    @BindView(R.id.tv_index) TextView tvIndex;

    ViewHolder(View view) {
      super(view);

      ivMore.setVisibility(mIsShowSelect ? View.VISIBLE : View.GONE);
      tvContent3.setVisibility(mIsShowSelect ? View.GONE : View.VISIBLE);
      if (mIconRes != 0) ivMore.setImageResource(mIconRes);
      ivMore.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
          showPopMenu(v);
        }
      });
    }
  }
}
